import request from '@/utils/request';
import type { Menu, Role } from '../entity'
import type {R} from '@/modules/common/entity';

const API_URL = '/manager/menuface';

export class MenuApi {
  // 创建新菜单
  async create(menu: Menu): Promise<R<Menu>> {
    return request({
      url: API_URL,
      method: 'post',
      data: menu
    })
  }

  // 根据ID获取菜单信息
  async getById(id: number): Promise<R<Menu>> {
    return request({
      url: `${API_URL}/${id}`,
      method: 'get'
    })
  }

  // 更新指定ID的菜单信息
  async update(id: number,menu: Menu): Promise<R<Menu>> {
    return request({
      url: `${API_URL}/${id}`,
      method: 'put',
      data: menu
    })
  }

  // 删除指定ID的菜单
  async delete(id: number): Promise<R<void>> {
    return request({
      url: `${API_URL}/${id}`,
      method: 'delete'
    })
  }

  // 获取所有菜单
  async getAll(): Promise<R<Menu[]>> {
    return request({
      url: `${API_URL}`,
      method: 'get'
    })
  }

  // 获取操作数量
  async count(): Promise<R<number>> {
    return request({
      url: `${API_URL}/count`,
      method: 'get',
    })
  }

  // 分页获取菜单
  async page(page: number,size: number): Promise<R<Menu[]>> {
    return request({
      url: `${API_URL}/page`,
      method: 'get',
      params: {page, size}
    })
  }

  // 获取操作的角色
  async roles(menuId?: number): Promise<R<Role[]>> {
    return request({
      url: `${API_URL}/roles`,
      method: 'get',
      params: {menuId}
    })
  }
}